///////////////////////////////////////////////////////////////////////////////
// File:        TcpServer.hpp
// Author:      671643387@qq.com
// Description:
///////////////////////////////////////////////////////////////////////////////

#ifndef MONKEY_COMMON_TCPSERVER_HPP_
#define MONKEY_COMMON_TCPSERVER_HPP_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <pio/server.h>
#include <string>
#include "common/TcpConnection.hpp"

namespace monkey
{
    namespace common
    {
        class TcpServer
        {
        public:
            TcpServer(int worker = 0);
            ~TcpServer(void);

        public:
            bool Open(std::string address, short port, int backlog);
            void Stop(void);

        protected:
            virtual void OnAcceptNewConnection(TcpConnection* conn);

        private:
            friend class TcpConnection;
            virtual TcpConnection* MallocConnection(struct pio_connection* conn);
            virtual void FreeConnection(TcpConnection* conn);

        private:
            static void _AcceptNewConnection(struct pio_connection* conn, void* arg);

        private:
            struct pio_server* server_;
        };
    }
}

#endif /* MONKEY_COMMON_TCPSERVER_HPP_ */
